 //
//  MeHeaderView.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/8.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  项目--我的--表格头视图

import UIKit


protocol MeHeaderDelegare {
    func jumpToLogin()
}

class MeHeaderView: UIView {

    var delegate : MeHeaderDelegare?
    
    //根据tag取出label
    var label = UILabel()
    
    //根据tag取出头像
    var headImg = UIImageView()
    
    override func awakeFromNib() {
        //接收通知
        listenNotifi()

    }
    

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.


    override func draw(_ rect: CGRect) {
        // Drawing code
        self.frame.size = CGSize(width: UIScreen.main.bounds.width, height: 150 )
        
        //根据tag取文本
        setLabelWithTag()
        //根据tag取头像
        setImgWithTag()
        

//        //退出
//        logout()

    }
    
    
}
 

 
// MARK:- 监听通知
extension MeHeaderView {
    ///监听通知
    func listenNotifi() -> Void {
        
        //接受微信通知监听
        let notifyChatMsgRecvWX = NSNotification.Name(rawValue:wxNotifi)
        
        NotificationCenter.default.addObserver(self, selector:#selector(headredWX(notification:)),name: notifyChatMsgRecvWX, object: nil)
        
        //接受QQ通知监听
        let notifyChatMsgRecvQQ = NSNotification.Name(rawValue:qqNotifi)
        
        NotificationCenter.default.addObserver(self, selector:#selector(headredQQ(notification:)),name: notifyChatMsgRecvQQ, object: nil)
        
        
        let heardLogout = NSNotification.Name(rawValue:logoutNotifi)
        NotificationCenter.default.addObserver(self, selector:#selector(logout),name: heardLogout, object: nil)

    }
    
    //通知处理微信登陆函数
    func headredWX(notification:NSNotification){
        print("bhbhbhbhb")
        print("didMsgRecv: \(notification.userInfo)")
        
        guard let wxReceiveDataSource = notification.userInfo as? [String : Any] else {
            return
        }
        
        let wxreiveDic = wxReceiveDataSource as NSDictionary
        //取出微信头像
        let wxHeadImgURL = wxreiveDic["wxImgUrl"] as! String
        

        //更新头像
        DispatchQueue.main.async {
            do {
                self.headImg.image = try UIImage(data: Data(contentsOf: URL(string: wxHeadImgURL)!))
                
            } catch {
                
            }
        }
        
        //取出微信名称
        let wxNickName = wxreiveDic["wxNickName"] as! String
        
        //更新名称
        label.text = wxNickName

    
    }
    
    //通知处理微信QQ函数
    func headredQQ(notification:NSNotification){
        print("bhbhbhbhb")
        print("didMsgRecv: \(notification.userInfo)")
        
        guard let qqReceiveDataSource = notification.userInfo as? [String : Any] else {
            return
        }
        
        let qqreiveDic = qqReceiveDataSource as NSDictionary
        //取出微信头像
        let qqHeadImgURL = qqreiveDic["qqImgUrl"] as! String
        
        
        //更新头像
        DispatchQueue.main.async {
            do {
                self.headImg.image = try UIImage(data: Data(contentsOf: URL(string: qqHeadImgURL)!))
                
            } catch {
                
            }
        }
        
        //取出微信名称
        let qqNickName = qqreiveDic["qqNickName"] as! String
        
        //更新名称
        label.text = qqNickName

    }
}


//表格透视图头事件
extension MeHeaderView {
    ///电话号码
    func setLabelWithTag() -> Void {

        label = viewWithTag(myHeadTelTag) as! UILabel
        label.isUserInteractionEnabled = true

        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(MeHeaderView.clickLabel))
        label.addGestureRecognizer(tapGes)
    }
    
    ///头像
    func setImgWithTag() -> Void {
        
        headImg = viewWithTag(myHeadImgTag) as! UIImageView
        headImg.isUserInteractionEnabled = true
        
        
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(MeHeaderView.clickImg))
        headImg.addGestureRecognizer(tapGes)

    }
    
    
    
    ///退出
    func logout() -> Void {
//        let logoutBtn = viewWithTag(myheadLogout) as! UIButton
//        logoutBtn.addTarget(self, action: #selector(logoutSEL), for: .touchUpInside)
//        NotificationCenter.default.addObserver(self, selector: #selector(heardLogout), name: logout, object: nil)
        print("收到退出通知")
        
        //移除通知函数
        NotificationCenter.default.removeObserver(self)
        
        label.text = "单机登陆"
        headImg.image = UIImage.init(named: "qq")

        }
    
}

 
// MARK:- 点击文字label进行登陆
extension MeHeaderView {
    func clickLabel() -> Void {
        print("单机表头视图标签进行登陆")
        delegate?.jumpToLogin()
        
        let uVC = UserVC()
        UIApplication.shared.keyWindow?.rootViewController?.navigationController?.pushViewController(uVC, animated: true)
    }
    
    func clickImg() -> Void {
        print("单机头像")
    }
    
    func clickInfoLabel() -> Void {
        print("单机信息")
        let infoVC = PPPViewController()
        UIApplication.shared.keyWindow?.rootViewController?.navigationController?.pushViewController(infoVC, animated: true)
    }
}

extension MeHeaderView {
    func heardLogout() -> Void {
        print("收到退出通知")
    }
}
 
extension MeHeaderView {
    func chnageNam() {
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        label.text = "登陆成功"
    }
}


